FROM php:7.2.34-fpm-buster

RUN cat /etc/apt/sources.list
RUN cat > /etc/apt/sources.list \
    && echo "deb http://mirrors.aliyun.com/debian/ buster main non-free contrib" >> /etc/apt/sources.list \
    && echo "deb http://mirrors.aliyun.com/debian-security buster/updates main" >> /etc/apt/sources.list \
    && echo "deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >> /etc/apt/sources.list \
    && echo "deb-src http://mirrors.aliyun.com/debian-security buster/updates main" >> /etc/apt/sources.list \
    && echo "deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib" >> /etc/apt/sources.list \
    && echo "deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >> /etc/apt/sources.list

RUN apt-get update -y && apt-get upgrade -y

RUN apt-get install -y wget openssl libssl-dev libcurl4-openssl-dev \
    && docker-php-ext-install -j$(nproc) bcmath calendar pcntl pdo_mysql \
    && pecl install redis \
    && docker-php-ext-enable redis
    #&& docker-php-ext-install opcache \
RUN echo "* soft nofile 655360" >> /etc/security/limits.conf \
    && echo "* hard nofile 655360" >> /etc/security/limits.conf
    # nginx start
RUN apt-get install -y nginx \
    && sed -i 's/include \/etc\/nginx\/sites-enabled\/\*;/#/g' "/etc/nginx/nginx.conf" \
    && rm -f /etc/nginx/sites-available/default
    # composer start
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
    && php composer-setup.php \
    && php -r "unlink('composer-setup.php');"\
    && mv composer.phar /usr/local/bin/composer \
    && chmod +x /usr/local/bin/composer


    # mongodb start
RUN pecl install https://pecl.php.net/get/mongodb-1.16.2.tgz \
    && docker-php-ext-enable mongodb
    # rdkafka
RUN apt-get install  -y librdkafka-dev \
    && pecl install rdkafka \
    && docker-php-ext-enable rdkafka \
    && mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
    && rm -f /usr/local/etc/php-fpm.d/zz-docker.conf

# swoole
ENV swoole_src=swoole-src-4.8.12
COPY thirdparty/${swoole_src}.tar.gz /tmp/
RUN  cd /tmp \
    && tar zxf ${swoole_src}.tar.gz && rm -f ${swoole_src}.tar.gz \
    && cd ${swoole_src} \
    && phpize && ./configure --enable-http2 --enable-openssl \
    && make && make install \
    && rm -rf /tmp/${swoole_src} \
    && docker-php-ext-enable swoole

COPY nginx/site.conf /etc/nginx/conf.d/site.conf

#COPY html /var/www/html
#COPY php/conf.d/docker-php-ext-opcache.ini /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini

RUN mkdir -p /var/log/php \
    && chown -R www-data:www-data /var/log/php
COPY php/php-fpm.d/www.conf /usr/local/etc/php-fpm.d/www.conf
COPY php/php-fpm.conf /usr/local/etc/php-fpm.conf

RUN sed -i 's/expose_php = On/expose_php = Off/g' "$PHP_INI_DIR/php.ini" \
    && sed -i 's/# server_tokens off/server_tokens off/g' /etc/nginx/nginx.conf \
    && sed -i "s/;error_log = php_errors.log/error_log = \/var\/log\/php\/php_errors.log/" "$PHP_INI_DIR/php.ini" \
    && sed -i "s/;date.timezone =/date.timezone = PRC/" "$PHP_INI_DIR/php.ini"

STOPSIGNAL SIGQUIT
EXPOSE 80
CMD ["php-fpm", "-F"]

COPY entrypoint.sh /etc/entrypoint.sh
RUN chmod +x /etc/entrypoint.sh
ENTRYPOINT ["/etc/entrypoint.sh"]
